

/**
 * @description 根据城市名设置地图中心点
 * @param {参数 zoom 缩放级别} 
 * @param {参数 cityName 城市名称} 
 * @param {参数 Instance 地图实例} 
 */
const setMapCenterAndZoom = ({
    Instance = null,
    cityName = "广州市",
    zoom = 12
}) => {
    Instance.centerAndZoom(cityName, zoom); // 初始化地图,设置中心点坐标和地图级别
}


/**
 * @description 添加区域掩膜
 * @param {参数 Instance 实例}
 * @param {参数 cityName 省市区}
 * @param {参数 zoom 缩放级别}
 */
const addMapMask = ({
    Instance = null,
    cityName = new BMapGL.Point(116.20596467080586, 40.0144555532275),
    zoom = 12,
}) => {
    Instance.centerAndZoom(cityName, zoom);
    Instance.enableScrollWheelZoom();


    var bdary = new BMapGL.Boundary();
    bdary.get(cityName, function (rs) {
        // 绘制行政区
        for (var i = 0; i < rs.boundaries.length; i++) {
            var path = [];
            var xyArr = rs.boundaries[i].split(';');
            var ptArr = [];
            for (var j = 0; j < xyArr.length; j++) {
                var tmp = xyArr[j].split(',');
                var pt = new BMapGL.Point(tmp[0], tmp[1]);
                ptArr.push(pt);
            }
            var mapmask = new BMapGL.MapMask(ptArr, {
                isBuildingMask: true,
                isPoiMask: true,
                isMapMask: true,
                showRegion: 'inside',
                topFillColor: '#5679ea',
                topFillOpacity: 0.5,
                sideFillColor: '#5679ea',
                sideFillOpacity: 0.9
            });
            Instance.addOverlay(mapmask);
            var border = new BMapGL.Polyline(ptArr, {
                strokeColor: '#4ca7a2',
                strokeWeight: 3,
                strokeOpacity: 1
            });
            Instance.addOverlay(border);
        }
    });

}

/**
 * @description 添加mark标记
 * @param {参数 Instance 实例}
 * @param {参数 cityName 省市区}
 * @param {参数 zoom 缩放级别}
 */
const addMark = ({
    Instance = null,
    point = null,
    marker = null,
    cityName = String,
    markerIcon = {}
}) => {
    var marker = new BMapGL.Marker(point, {
        ...markerIcon
    });
    // Instance.centerAndZoom(point, 15);
    // 将标注添加到地图
    Instance.addOverlay(marker);
}


/**
 * @description 创建marker图标
 * @param { 参数 imgUrl 用以生成marker图标的图片地址 }
 */
const getMarkerIcon = ({
    imgUrl = String,
    Size = [52, 26]
}) => {
    // 创建小车图标
    var icon = new BMapGL.Icon(imgUrl, new BMapGL.Size(Size[0], Size[1]));
    return icon;
}

/**
 * @description 地址解析，获取point
 */
const getPoint = ({
    address = String,
    callBack = null
}) => {
    //创建地址解析器实例
    var myGeo = new BMapGL.Geocoder();
    myGeo.getPoint(address, function (point) {
        if (point) {
            callBack && callBack(point);
            return point;
        }
    })
}

export {
    setMapCenterAndZoom,
    addMapMask,
    addMark,
    getPoint,
    getMarkerIcon
}